package com.alks.function.controller.biocontrol.factoryquery;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.DarkLock;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DatabaseUtils.DatabaseUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.biocontrol.factoryQuery.StkAccDto;
import com.alks.function.data.request.biocontrol.factoryQuery.StkAccRequest;
import com.alks.function.service.biocontrol.factoryQuery.IStkAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author MS
 * @since 2024/11/7下午5:21
 */
@RestController
@ApiRestController("/fq/StkAcc")
@Api(value = "生控部", tags = "生控部-工厂查询-库存账目查询")
@Slf4j
public class StkAccController {
    @Autowired
    private IStkAccService service;

    @WebLogger
    @PostMapping("/temp")
    @ApiOperation(value = "首页数据", tags = "生控部-工厂查询-库存账目查询")
    public ResponseInfo temp(@Valid @RequestBody StkAccRequest request) {
        DatabaseUtils.sqlCheck(request.getOrderFile());
        DatabaseUtils.sqlCheck(request.getPositiveFile());
        PageRecord<StkAccDto> record = service.temp(request);
        return ResponseInfo.ok(record);
    }

    @WebLogger
    @GetMapping("/update/forced")
    @ApiOperation(value = "数据刷新", tags = "库存账目查询")
    public ResponseInfo stkAccUpdateForced(@RequestParam(required = false) Integer day) {
        service.updateForced(day,1);
        return ResponseInfo.ok();
    }


    @WebLogger
    @PostMapping("/export/process")
    @ApiOperation(value = "工艺日报表导出(冲裁，外协，配套)", tags = "生控部-工厂查询-库存账目查询")
    public void processExport(@Valid @RequestBody StkAccRequest request, HttpServletResponse response) throws IOException {
        /*准备数据*/
        request.setPageSize(-1);
        request.setExportFlag(true);
        String templetName = "冲裁日报表模版.xls";
        String excelName = "冲裁日报表" + request.getDdate().format(DateTimeFormatter.ofPattern("yyMMdd")) + ".xls";
        PageRecord<StkAccDto> record = service.temp(request);
        List<StkAccDto> accDtoList = record.getList();
        Map<String,String> map =new HashMap<>(1);
        map.put("date", request.getDdate().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
        /*写入EXCEL*/
        try {
            ExcelUtils.creatByTemplate(response, templetName, excelName ,accDtoList,map);
        } catch (IOException e) {
            throw e;
        }
    }

}